# coding=utf-8
import json

from worldcup.apps.userModel.models import Spu, Sku
from worldcup.commons.CommonResult import rest, CommonResult


@rest
def list_spu(request):
	category_id = request.GET.get("categoryId")
	if category_id is None:
		return CommonResult(-1, "category_id不能为空")
	country = request.GET.get("country")
	spu_sql = Spu.objects.filter(category_id=category_id, spu_image__isnull=False).exclude(status=0)
	if country is not None:
		spu_sql = spu_sql.filter(spu_name__contains=country)
	spus = spu_sql.order_by("-order").all()
	results = []
	for spu in spus:
		# 获取spu最低价格
		sku = Sku.objects.values("price").filter(spu_code=spu.spu_code).order_by("price")[0]
		result = {
			"spuCode": spu.spu_code,
			"spuName": spu.spu_name,
			"spuImage": spu.spu_image,
			"status": spu.status,
			"minPrice": str(sku["price"]),
			"isHot": spu.is_hot,
			"saleVolume": spu.sale_volume,
			"status": spu.status
		}

		if spu.spu_ext_properties is not None and len(spu.spu_ext_properties) > 0:
			ext_properties_dict = json.loads(spu.spu_ext_properties)
			for k, v in ext_properties_dict.items():
				result[k] = v
		results.append(result)
	return CommonResult(0, results)


@rest
def get_spu(request):
	spu_code = request.GET.get("spuCode")
	if spu_code is None:
		return CommonResult(-1, "spuCode不能为空")
	spus = Spu.objects.filter(spu_code=spu_code).all()
	if len(spus) == 0:
		return CommonResult(0, {})
	spu = spus[0]
	skus = Sku.objects.filter(spu_code=spu_code).all()
	result = {
		"spuCode": spu.spu_code,
		"spuName": spu.spu_name,
		"spuDescription": spu.spu_description,
		"spuImage": spu.spu_image,
		"descImgs": None if spu is None else spu.desc_imgs.split(","),
		"status": spu.status,
		"specificationName": None if spu.specification_name is None or spu.specification_name == "" else json.loads(spu.specification_name),
		"saleVolume": spu.sale_volume,
	}
	if spu.spu_ext_properties is not None and len(spu.spu_ext_properties) > 0:
		ext_properties_dict = json.loads(spu.spu_ext_properties)
		for k, v in ext_properties_dict.items():
			result[k] = v

	# 找到全部的规格
	specifications = {}
	sku_results = []
	for sku in skus:
		sku_result = {
			"skuCode": sku.sku_code,
			"skuName": sku.sku_name,
			"skuImages":  list() if sku.sku_images is None or sku.sku_images == "" else sku.sku_images.split(","),
			"price": str(sku.price),
			"originPrice": None if sku.origin_price is None else str(sku.origin_price),
			"inventoryNum": sku.inventory_num,
			"saleUnit": sku.sale_unit,
			"saleUnitName": sku.sale_unit_name,
			"specification": sku.sku_specification,
		}
		sku_results.append(sku_result)
		# 保存规格
		sku_specification = sku.sku_specification
		if sku_specification is not None and len(sku_specification) > 0:
			specification_dict = json.loads(sku_specification)
			for k, v in specification_dict.items():
				k_list = specifications.get(k)
				if k_list is None:
					k_list = list()
					specifications[k] = k_list
				if v not in k_list:
					k_list.append(v)
	for k, v in specifications.items():
		specifications[k] = list(v)
	result["skuSpecifications"] = specifications
	result["skus"] = sku_results

	return CommonResult(0, result)
